let host = "http://localhost:8080"

/**
 * 数据请求
 * @param {*} url 接口地址
 * @param {*} method 请求方式
 * @param {*} data 携带数据
 * @param {*} header 请求头
 * 
 */
function request(url, method = 'POST', data = {}, header = {
	'Content-Type': 'application/json'
}) {
	return new Promise((resolve, reject) => {
		wx.request({
			url: host + url,
			data,
			method,
			header,
			success: res => resolve(res.data),
			fail: err => reject(err)
		})
	})
}

module.exports = {

	// 获取歌曲url/歌词
	async getSongPlay(data) {
		try {
			return await request(`/play`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 获取歌曲url/歌词（已有替代，暂停使用）
	async getSongPlay2(songType, songId) {
		return new Promise((resolve, reject) => {
			wx.request({
				url: "http://www.6uq.cn/static/js/play.php",
				method: "POST",
				header: {
					"Content-Type": "application/x-www-form-urlencoded"
				},
				data: {
					id: songId,
					type: "music"
				},
				success: res => {
					resolve({
						code: 200,
						data: res.data
					})
				},
				fail: err => {
					resolve({
						code: 201,
						msg: "访问出错"
					})
				}
			})
		})
	},
	// 获取mv视频url
	async getVideoUrl(data) {
		try {
			return await request(`/video`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 首页获取列表
	async getAll() {
		try {
			return await request(`/`, 'GET')
		} catch (error) {
			return {
				code: 201,
				msg: '访问出错'
			}
		}
	},
	// 热门歌单：类别
	async getPlayListType() {
		try {
			return await request(`/hotSong/type`, 'GET')
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 热门歌单：类别——歌单
	async getHotSong(data) {
		try {
			return await request(`/hotSong`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 热门歌单：类别——歌单——曲目
	async getHotSongList(data) {
		try {
			return await request(`/hotSong/list`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 榜单列表
	async getBangType() {
		try {
			return await request(`/bang`, 'GET')
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 榜单列表的曲目
	async getBangList(data) {
		try {
			return await request(`/bang/list`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 热门歌手
	async getHotSingerList(data) {
		try {
			return await request(`/hotSinger`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 热门歌手的曲目
	async getHotSingerSongList(data) {
		try {
			return await request(`/hotSinger/song`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 大家在听
	async getPlayLog() {
		try {
			return await request(`/playLog`, 'GET')
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 热搜词
	async getHotSearchWord() {
		try {
			return await request(`/hotSearchWord`, 'GET')
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 搜索内容
	async search(data) {
		try {
			return await request(`/search`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 新碟专辑
	async getAlbum() {
		try {
			return await request(`/album`, 'GET')
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 新碟专辑曲目列表
	async getAlbumList(data) {
		try {
			return await request(`/album/list`, 'POST', data)
		} catch (error) {
			return {
				code: 201,
				msg: "访问出错"
			}
		}
	},
	// 下载地址的重定向（暂停使用）
	async downloadUrlRedirect(ac, id, type) {
		// ac:dmp3（默认）。id:歌曲id。type:mp3(标准音质) / 320(极高音质) / flac(FLAC无损)
		return new Promise((resolve, reject) => {
			let requestTask = wx.request({
				url: `http://www.6uq.cn/plug/down.php?ac=${ac}&id=${id}&type=${type}`,
				method: 'GET',
				redirect: "manual", //	拦截重定向
			})
			requestTask.onHeadersReceived((res) => {
				console.log(res.header.Location)
				resolve(res.header.Location)
			})
		})
	},
}